﻿using System;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class Pages_Carrier_RouteEditStations : System.Web.UI.Page
{
	Int64 Route_id { get { return DS.ToInt64(Request.QueryString["Id"]); } }

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
			BindControls();
	}

	private void BindControls()
	{
		Route r = DBEntity<Route>.Load(Route_id);
		if (r == null)
			return;
		aReturnToRoute.HRef = string.Format("RouteEdit.aspx?Id={0}",Route_id);
		lbRouteName.Text = r.Name;
		RouteStationM2M rsM2M = new RouteStationM2M();
		rsM2M.route.Id = Route_id;
		rpt1.DataSource = DBEntity<RouteStationM2M>.Select(rsM2M);
		rpt1.DataBind();
	}

	protected void OnRouteStationsSave(object s, EventArgs e)
	{
		bool IsSaved = true;
		for (int i = 1; i < (rpt1.Controls.Count-1); i++)
		{
			RouteStationM2M rsM2M = DBEntity<RouteStationM2M>.Load(DS.ToInt64((rpt1.Controls[i].Controls[1] as HiddenField).Value));
			rsM2M.Distance = DS.ToDouble((rpt1.Controls[i].Controls[3] as TextBox).Text);
			rsM2M.TimeDistance = DS.ToDouble((rpt1.Controls[i].Controls[7] as TextBox).Text);
			rsM2M.LandingPlace = (rpt1.Controls[i].Controls[11] as TextBox).Text;
			IsSaved = IsSaved && rsM2M.Merge();
		}
		if (IsSaved)
		{
			lbMessage.ForeColor = Color.Green;
			lbMessage.Text = "Данные сохранены успешно";
		}
		else
		{
			lbMessage.ForeColor = Color.Red;
			lbMessage.Text = "При сохранении данных проихошла ошибка";
		}
		BindControls();
	}

	protected void OnCancel(object s, EventArgs e)
	{
		Response.Redirect(string.Format("RouteEdit.aspx?Id={0}", Route_id));
	}
}
